import { url } from '../utils'
import { map, filter } from 'lodash'
const state = {
  assetCategories: [],
  assetStatus: [],
  assetBrands: [],
  assetAffiliations: [],
  assetKeepers: [],
  assetVendors: [],
  assetLocations: [],
  assetStockChangeHistoryActions: []
}

const pivotMap = {
  brand: 'assetBrands',
  affiliation: 'assetAffiliations',
  vendor: 'assetVendors',
  keeper: 'assetKeepers',
  location: 'assetLocations'
}

const mutations = {
  pushBrand(state, Brand) {
    state.assetBrands.push(Brand)
  },
  pushAffiliation(state, Affiliation) {
    state.assetAffiliations.push(Affiliation)
  },
  pushKeeper(state, Keeper) {
    state.assetKeepers.push(Keeper)
  },
  pushVendor(state, Vendor) {
    state.assetVendors.push(Vendor)
  },
  pushLocation(state, Location) {
    state.assetLocations.push(Location)
  },
  refreshPivot(state, { name, meta }) {
    state[name] = meta
  },
  refreshMeta(state, data) {
    state.assetCategories = data.assetCategories
    state.assetStatus = data.assetStatus
    state.assetBrands = data.assetBrands
    state.assetAffiliations = data.assetAffiliations
    state.assetKeepers = data.assetKeepers
    state.assetVendors = data.assetVendors
    state.assetLocations = data.assetLocations
    state.assetStockChangeHistoryActions = data.assetStockChangeHistoryActions
  }
}

const actions = {
  save(state, asset) {
    return axios.post(url('/api/asset'), asset)
  },
  delete(state, id){
    return axios.post(url(`/api/asset/delete/${id}`))
  },
  downloadAssetImportTemplate() {
    return axios({
      url: url('/api/asset/excel/export_template'),
      method: 'GET',
      responseType: 'blob' // important
    })
  },
  downloadExcel(){
    return axios({
      url: url('/api/asset/excel/export'),
      method: 'GET',
      responseType: 'blob' // important
    })
  },
  fetchAssetList: function (state, payload) {
    return axios.post(url('/api/asset/list'), payload)
  },
  import(state, importFile) {
    let formData = new FormData()
    formData.append('upload_file', importFile)
    return axios.post(url('/api/asset/excel/import'), formData, {
      headers: {
        'Content-Type': 'multipart/form-data'
      }
    })
  },
  fetchAssetMeta(ctx, { name }) {
    return axios.get(url(`/api/asset/meta/${name}`)).then(({ data }) => {
      ctx.commit('refreshPivot', {
        name: pivotMap[name],
        meta: data.data.meta
      })
    })
  },
  bootstrap(ctx) {
    // 初始化  由于会发送ajax所以这里会自动验证token是否有效情况
    return new Promise((resolve, reject) => {
      axios.get(url('/api/meta/asset-meta')).then(({ data }) => {
        ctx.commit('refreshMeta', data)
        resolve()
      })
    })
  },
  store(context, { formData }) {
    let postUrl = url(`/api/asset/${payload.action}`)
    return axios.post(postUrl, formData)
  }
}

const getters = {
  primaryCategories(state) {
    return filter(state.assetCategories, item => item.is_primary)
  }
}

export default {
  namespaced: true,
  state,
  mutations,
  actions,
  getters
}
